/**
 * 测试下注记录状态显示优化
 * 验证状态0显示"未开奖"，状态1显示"结算中"
 */

const { Client } = require('pg');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  port: 5432,
  database: 'your_database_name',
  user: 'your_username',
  password: 'your_password'
};

// 模拟getBetTypeDescription方法
function getBetTypeDescription (betType) {
  const descriptions = {
    1: '大',
    2: '小',
    3: '单',
    4: '双',
    5: '豹子'
  };
  return descriptions[betType] || '未知';
}

// 模拟优化后的状态显示逻辑
function getBetDescription (bet) {
  const betTypeDesc = getBetTypeDescription(bet.betType);
  const currencyDesc = bet.currencyType === 1 ? "金" : "银";

  let statusDesc;
  if (bet.status === 0) {
    statusDesc = "未开奖";
  } else if (bet.status === 1) {
    statusDesc = "结算中";
  } else if (bet.status === 2 && bet.winAmount > 0) {
    const winCurrency = bet.currencyType === 1 ? "金" : "银两";
    statusDesc = "赢" + bet.winAmount + winCurrency;
  } else {
    statusDesc = "输局";
  }

  return `${betTypeDesc}${bet.betAmount}${currencyDesc},${statusDesc}!`;
}

async function testBetStatusDisplay () {
  const client = new Client(dbConfig);

  try {
    await client.connect();
    console.log('✅ 数据库连接成功');

    // 测试大小玩法下注记录状态显示
    console.log('\n🔍 测试大小玩法下注记录状态显示...');

    const diceBetsQuery = `
      SELECT 
        id, round_id, bet_type, bet_amount, currency_type, 
        status, win_amount, created_at
      FROM casino_dice_bets 
      ORDER BY id DESC 
      LIMIT 10
    `;

    const diceBets = await client.query(diceBetsQuery);
    console.log(`找到 ${diceBets.rows.length} 条大小玩法下注记录`);

    diceBets.rows.forEach(bet => {
      const description = getBetDescription(bet);
      const statusName = bet.status === 0 ? '下注中' :
        bet.status === 1 ? '已开奖' : '已结算';

      console.log(`下注ID ${bet.id}: ${description}`);
      console.log(`  状态: ${statusName}(${bet.status})`);
      console.log(`  下注: ${bet.bet_type} ${bet.bet_amount} ${bet.currency_type === 1 ? '金' : '银'}`);
      if (bet.win_amount > 0) {
        console.log(`  中奖: ${bet.win_amount} ${bet.currency_type === 1 ? '金' : '银'}`);
      }
      console.log('');
    });

    // 测试赤壁玩法下注记录状态显示
    console.log('\n🔍 测试赤壁玩法下注记录状态显示...');

    const chibiBetsQuery = `
      SELECT 
        id, round_id, side, bet_gold, bet_silver, 
        status, win_gold, win_silver, created_at
      FROM casino_chibi_bets 
      ORDER BY id DESC 
      LIMIT 10
    `;

    const chibiBets = await client.query(chibiBetsQuery);
    console.log(`找到 ${chibiBets.rows.length} 条赤壁玩法下注记录`);

    chibiBets.rows.forEach(bet => {
      const sideName = bet.side === 1 ? '魏国' :
        bet.side === 2 ? '蜀国' : '吴国';
      const totalBet = (bet.bet_gold || 0) + (bet.bet_silver || 0);
      const totalWin = (bet.win_gold || 0) + (bet.win_silver || 0);

      // 模拟赤壁玩法的描述格式
      let statusDesc;
      if (bet.status === 0) {
        statusDesc = "未开奖";
      } else if (bet.status === 1) {
        statusDesc = "结算中";
      } else if (bet.status === 2 && totalWin > 0) {
        statusDesc = `赢${totalWin}${bet.win_gold > 0 ? '金' : '银'}`;
      } else {
        statusDesc = "输局";
      }

      const description = `${sideName}${totalBet}${bet.bet_gold > 0 ? '金' : '银'},${statusDesc}!`;
      const statusName = bet.status === 0 ? '下注中' :
        bet.status === 1 ? '已开奖' : '已结算';

      console.log(`下注ID ${bet.id}: ${description}`);
      console.log(`  状态: ${statusName}(${bet.status})`);
      console.log(`  下注: ${sideName} ${bet.bet_gold}金 ${bet.bet_silver}银`);
      if (totalWin > 0) {
        console.log(`  中奖: ${bet.win_gold}金 ${bet.win_silver}银`);
      }
      console.log('');
    });

    // 测试各种状态组合
    console.log('\n🧪 测试各种状态组合...');

    const testCases = [
      { status: 0, winAmount: 0, betType: 1, betAmount: 100, currencyType: 1, desc: '下注中，未开奖' },
      { status: 1, winAmount: 0, betType: 2, betAmount: 50, currencyType: 2, desc: '已开奖，结算中' },
      { status: 2, winAmount: 200, betType: 1, betAmount: 100, currencyType: 1, desc: '已结算，中奖' },
      { status: 2, winAmount: 0, betType: 2, betAmount: 50, currencyType: 2, desc: '已结算，未中奖' },
    ];

    testCases.forEach((testCase, index) => {
      const description = getBetDescription(testCase);
      console.log(`测试用例 ${index + 1}: ${testCase.desc}`);
      console.log(`  输入: status=${testCase.status}, winAmount=${testCase.winAmount}`);
      console.log(`  输出: ${description}`);
      console.log('');
    });

    // 统计各状态的记录数
    console.log('\n📊 状态统计:');

    const diceStatusCounts = diceBets.rows.reduce((acc, bet) => {
      acc[bet.status] = (acc[bet.status] || 0) + 1;
      return acc;
    }, {});

    const chibiStatusCounts = chibiBets.rows.reduce((acc, bet) => {
      acc[bet.status] = (acc[bet.status] || 0) + 1;
      return acc;
    }, {});

    console.log('大小玩法状态分布:');
    Object.entries(diceStatusCounts).forEach(([status, count]) => {
      const statusName = status === '0' ? '下注中(未开奖)' :
        status === '1' ? '已开奖(结算中)' : '已结算';
      console.log(`  ${statusName}: ${count} 条`);
    });

    console.log('赤壁玩法状态分布:');
    Object.entries(chibiStatusCounts).forEach(([status, count]) => {
      const statusName = status === '0' ? '下注中(未开奖)' :
        status === '1' ? '已开奖(结算中)' : '已结算';
      console.log(`  ${statusName}: ${count} 条`);
    });

    console.log('\n🎉 下注记录状态显示优化测试完成！');

  } catch (error) {
    console.error('❌ 测试失败:', error.message);
  } finally {
    await client.end();
  }
}

// 运行测试
if (require.main === module) {
  testBetStatusDisplay().catch(console.error);
}

module.exports = { testBetStatusDisplay };
